package gameEngine.agents.choosingStrategy;

import java.util.HashMap;
import gameEngine.agents.choosingStrategy.EnumOfStrategy.Strategy_Type;
import gameEngine.agents.choosingStrategy.representation.ManuallAdaptiveRepresentation;
import gameEngine.agents.choosingStrategy.representation.StrategyRepresentation;
import org.apache.log4j.Logger;
import Data.HashMapesOfParameters;
/**
 * used to create strategies by UI
 * @author alon
 *
 */
public class StrategyFactory {
	static Logger log = Logger.getLogger(gameEngine.agents.choosingStrategy.StrategyFactory.class);
	
	/**
	 * Factory using the representation
	 * @param strRepresentation
	 * @return {@link ChoosingStrategy}
	 */
	public static ChoosingStrategy create(StrategyRepresentation strRepresentation){

		//get the maps with the data
		HashMap<String, Strategy_Type> strategyMap = HashMapesOfParameters.getStrategyMap();
		Strategy_Type type = strategyMap.get(strRepresentation.getStrategyName());
			
		switch(type){
		case E_GREEDY_STRATEGY:
			log.debug("Epsilon Greedy Strategy created");
			return new EGreedyStratagy();
		
		case SIMPLE_RANDOM_STRATEGY:
		log.debug("Random Strategy created");
		return new SimpleRandomStrategy();
		
		case WORST_STRATEGY:
			log.debug("worst Strategy created");
			return new SelectWorstStrategy();
			
		case BEST_STRATEGY:
			log.debug("best Strategy created");
			return new GreedyStrategy();
		
		case ROUND_ROBIN_STRATEGY:
			log.debug("Round Robin Strategy created");
			return new RoundRobinStrategy();
		
		case GREEDY_STRATEGY:
			log.debug("Greedy Strategy created");
			return new GreedyStrategy();
			
		case MANUAL_ADAPTIVE_STRATEGY:
			log.debug("ManualAdaptive Strategy created");
			ManuallAdaptiveStrategy manuallAdaptiveStrategy = new ManuallAdaptiveStrategy();
			setManualAdaptive(manuallAdaptiveStrategy, strRepresentation);
			return manuallAdaptiveStrategy;
			
	}
		log.debug("no strategy match for type "+type+"return Epsilon Greedy");
		return new EGreedyStratagy();
	}
	
	
	
	/**	
	 * for manual strategy
	 * @param manuallAdaptiveStrategy
	 * @param strategyRepresentation
	 */
	private static void setManualAdaptive(ManuallAdaptiveStrategy manuallAdaptiveStrategy,
				StrategyRepresentation strategyRepresentation)
	{
		ManuallAdaptiveRepresentation maAdaptiveRepresentation = (ManuallAdaptiveRepresentation) strategyRepresentation; 
		int numStrategies = maAdaptiveRepresentation.numberOfStrategies();
		
		// go over the strategies and set them to the manuallAdaptiveStrategy
		for(int i = 0; i < numStrategies; ++i)
		{
			// create the real strategy from representation
			ChoosingStrategy strategy = create(maAdaptiveRepresentation.getStrategy(i + 1));
			//insert to manual strategy
			manuallAdaptiveStrategy.addStrategy(strategy,
								maAdaptiveRepresentation.getNumberTurns(i + 1));
		}
			
	}
}
